﻿using Microsoft.EntityFrameworkCore;
using Rechargeable.Products;
using Volo.Abp.Data;
using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore.Modeling;
using Volo.Abp.Users.EntityFrameworkCore;
using Rechargeable.Machines;
using Rechargeable.Games;
using Volo.Abp.OpenIddict.EntityFrameworkCore;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Identity.EntityFrameworkCore;
using Volo.Abp.TenantManagement.EntityFrameworkCore;
using Volo.Abp.Identity;
using Volo.Abp.TenantManagement;
using Volo.Abp.AuditLogging.EntityFrameworkCore;
using Volo.Abp.BackgroundJobs.EntityFrameworkCore;
using Volo.Abp.FeatureManagement.EntityFrameworkCore;
using Volo.Abp.PermissionManagement.EntityFrameworkCore;
using Volo.Abp.SettingManagement.EntityFrameworkCore;

namespace Rechargeable.EntityFrameworkCore
{
    /* This is your actual DbContext used on runtime.
     * It includes only your entities.
     * It does not include entities of the used modules, because each module has already
     * its own DbContext class. If you want to share some database tables with the used modules,
     * just create a structure like done for AppUser.
     *
     * Don't use this DbContext for database migrations since it does not contain tables of the
     * used modules (as explained above). See RechargeableMigrationsDbContext for migrations.
     */
    [ReplaceDbContext(typeof(IIdentityDbContext))]
    [ReplaceDbContext(typeof(ITenantManagementDbContext))]
    [ConnectionStringName("Default")]
    public class RechargeableDbContext : AbpDbContext<RechargeableDbContext>, IIdentityDbContext,
    ITenantManagementDbContext
    {

        //Identity
        public DbSet<IdentityUser> Users { get; set; }
        public DbSet<IdentityRole> Roles { get; set; }
        public DbSet<IdentityClaimType> ClaimTypes { get; set; }
        public DbSet<OrganizationUnit> OrganizationUnits { get; set; }
        public DbSet<IdentitySecurityLog> SecurityLogs { get; set; }
        public DbSet<IdentityLinkUser> LinkUsers { get; set; }
        public DbSet<IdentityUserDelegation> UserDelegations { get; }

        // Tenant Management
        public DbSet<Tenant> Tenants { get; set; }
        public DbSet<TenantConnectionString> TenantConnectionStrings { get; set; }

        /* Add DbSet properties for your Aggregate Roots / Entities here.
         * Also map them inside RechargeableDbContextModelCreatingExtensions.ConfigureRechargeable
         */
        //public DbSet<AppUser> Users { get; set; }

        public DbSet<Product> Products { get; set; }

        public DbSet<ProductPrice> ProductPrices { get; set; }

        public DbSet<Machine> Machines { get; set; }

        public DbSet<MachineRecord> MachineRecords { get; set; }

        public DbSet<SerialNumber> SerialNumbers { get; set; }

        public DbSet<GameAccount> GameAccounts { get; set; }

        public DbSet<CardInformation> CardInformations { get; set; }


        public RechargeableDbContext(DbContextOptions<RechargeableDbContext> options)
            : base(options)
        {

        }

        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);

            /* Include modules to your migration db context */

            builder.ConfigurePermissionManagement();
            builder.ConfigureSettingManagement();
            builder.ConfigureBackgroundJobs();
            builder.ConfigureAuditLogging();
            builder.ConfigureIdentity();
            builder.ConfigureOpenIddict();
            builder.ConfigureFeatureManagement();
            builder.ConfigureTenantManagement();

            /* Configure the shared tables (with included modules) here */

            //builder.Entity<AppUser>(b =>
            //{
            //    b.ToTable("AbpUsers"); //Sharing the same table "AbpUsers" with the IdentityUser
            //    b.ConfigureByConvention();
            //    b.ConfigureAbpUser();

            //    //Moved customization to a method so we can share it with the RechargeableMigrationsDbContext class
            //    b.ConfigureCustomUserProperties();
            //});

            /* Configure your own tables/entities inside the ConfigureRechargeable method */

            builder.ConfigureRechargeable();
        }
    }
}
